

import Persistence.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author shkstart
 * @date 2019/5/30 - 17:30
 */
public class Demo {
    @Test
    public void fun1() throws IOException {
        InputStream in = Resources.getResourceAsStream("config/mybatis-config.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = build.openSession();
        List<User> user = sqlSession.selectList("mapper.UserMapper" + ".findByName", "王");

        System.out.println(user);/*toString也可以不用*/
        sqlSession.close();
    }

    @Test
    public void fun() throws Exception {/*IOException*/
        //获取sqlMapConfig.xml文件流,读取配置文件
        //String resource = "config/mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream("config/mybatis-config.xml");
        //创建工厂添加sqlMapConfig.xml
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //通过SqlSessionFactory创建SqlSession，获得session
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //SqlSession执行映射文件中的SQL语句，并且返回映射结果
        //selectOne("test.findUserById", 1)前面的的值UserMapper中的namespace的值+id名
        User user = sqlSession.selectOne("mapper.UserMapper" + ".findUserById", 1);
        //打印输出结果
        System.out.println(user.toString());
        //释放资源,关闭SqlSession
        sqlSession.close();
    }

    @Test
    public void fun2() throws IOException {
        InputStream in = Resources.getResourceAsStream("config/mybatis-config.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = build.openSession();
        User user = new User();
        user.setUsername("马云");
        user.setSex("1");
        user.setAddress("嘉兴");
        user.setSingle("0");
        /*值得注意的是sqlSession的insert方法返回的是SQL语句影响的行数*/
        int insert = sqlSession.insert("mapper.UserMapper" + ".UserAdd", user);
        //通过返回值结果判断插入操作是否执行成功
        if (insert > 0) {
            System.out.println("你已成功插入"+insert+"条数据");
        } else {
            System.out.println("执行插入失败！！");
        }
        //System.out.println(insert);
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void fun3() throws IOException {
        InputStream in = Resources.getResourceAsStream("config/mybatis-config.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = build.openSession();
        int delete = sqlSession.delete("mapper.UserMapper.UserDelete", 22);
        sqlSession.commit();
        System.out.println(delete);
        sqlSession.close();
    }
    @Test
    public void fun4() throws IOException {
        InputStream in = Resources.getResourceAsStream("config/mybatis-config.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = build.openSession();
        User user = new User();
        user.setId(21);
        user.setUsername("马化腾");
        user.setSex("1");
        user.setAddress("深圳");
        user.setSingle("0");
        int update = sqlSession.insert("mapper.UserMapper.UserUpdate", user);
        sqlSession.commit();
        System.out.println(update);
        sqlSession.close();
    }
}